#include <iostream>
#include <stdio.h>

#include "../arbol-b+/arbolbmas.cpp"
#include "../arbol-b+/iteradorbmas.cpp"
#include "../persistencia/serializable.h"
#include "../persistencia/keys/typekey.h"

class UIntKey : public TypeKey<u_int32_t> {
    public:
        UIntKey(){}
        UIntKey(u_int32_t id) : TypeKey<u_int32_t>(id) {}

        virtual u_int32_t hashcode(){
            return id;
        }
};

class TestRecordInt : public Serializable {
    public:
        TestRecordInt() { }

        TestRecordInt(u_int32_t id){
            key.set(id);
        }
        UIntKey& getKey(){
            return key;
        }
        virtual void read(BinaryStream& in) {
            key.read(in);
        }
        virtual void write(BinaryStream& out) {
            key.write(out);
        }
        virtual std::string toString() {
            std::stringstream sout;
            sout << key.get();
            return sout.str();
        }
    private:
        UIntKey key;
};

void testsBMasEnteros() {
    u_int32_t ids [38] = {30, 10, 20, 40, 900, 750, 35, 50, 15, 800, 150, 200, 400,
                          170, 45, 190, 650, 70, 100, 350, 250, 450, 55, 60, 180,
                          500, 80, 700, 65, 90, 120, 300, 550, 600, 850,  950, 575, 110};

    ArbolBMas<UIntKey, TestRecordInt> arbol("arbol.bin", 32);

    if ( arbol.isOpen()) {

        for (u_int32_t i = 0; i < 38; i++) {
            arbol.insertar(TestRecordInt(ids[i]));
        }

        std::cout << arbol.toString() << std::endl;

        IteradorBMas<UIntKey, TestRecordInt> iterador(arbol, UIntKey(200));
        Nullable<TestRecordInt> next = iterador.siguiente();
        while (next){
            std::cout << next.get().toString() << std::endl;
            next = iterador.siguiente();
        }

        remove("arbol.bin");
    }
}
